package com.iflytek.aisol.audit.iflytek.strategy.manage.handler.config;

import com.iflytek.aisol.audit.iflytek.strategy.manage.config.CustomException;
import com.iflytek.aisol.audit.iflytek.strategy.manage.constant.ChannelType;
import com.iflytek.aisol.audit.iflytek.strategy.manage.handler.ChannelHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;

@Component
public class ChannelHandlerFactory {
    @Autowired
    private ApplicationContext applicationContext;

    public ChannelHandler getHandler(String channelType) {
        ChannelType typeEnum = ChannelType.fromType(channelType);
        if (typeEnum == null) {
            throw new IllegalArgumentException("无效的channelType: " + channelType);
        }
        try {
            return applicationContext.getBean(typeEnum.getHandlerClass());
        } catch (Exception e) {
            throw new CustomException("Handler找不到对应的枚举值: " + channelType);
        }
    }
}
